package com.xiyu.service.repository.fee.deposit;

import com.xiyu.service.model.elder.info.ElderInfoFetcher;
import com.xiyu.service.model.fee.deposit.FeeDepositDetailFetcher;
import com.xiyu.service.vo.fee.feeDeposit.FeeDepositPageQueryDetailInput;
import com.xiyu.service.model.fee.deposit.FeeDepositDetail;
import com.xiyu.service.model.fee.deposit.FeeDepositDetailTable;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;

import java.util.List;

public interface FeeDepositDetailRepository extends JRepository<FeeDepositDetail, Long> {
    FeeDepositDetailTable feeDepositDetailTable = FeeDepositDetailTable.$;

    default Page<FeeDepositDetail> pageQueryDetail(FeeDepositPageQueryDetailInput inputVO){
        return sql().createQuery(feeDepositDetailTable)
                .whereIf(inputVO.getElderId() != null, feeDepositDetailTable.elderId().eq(inputVO.getElderId()))
                .whereIf(inputVO.getPayType() != null, feeDepositDetailTable.payType().eq(inputVO.getPayType()))
                .whereIf(inputVO.getStatus() != null, feeDepositDetailTable.status().eq(inputVO.getStatus()))
                .whereIf(inputVO.getPayTime() != null, ()-> feeDepositDetailTable.payTime().between(inputVO.getPayTime()[0], inputVO.getPayTime()[1]))
                .orderBy(feeDepositDetailTable.id().desc())
                .select(feeDepositDetailTable.fetch(FeeDepositDetailFetcher.$.allTableFields().elder(ElderInfoFetcher.$.allTableFields())))
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }

    List<FeeDepositDetail> findByElderId(Long elderId);

}